

interface IWorkExcelExportViewModel extends IBaseViewModel<IExportReportQueryTerms> {

    excelInfo: WorkExcelItem;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 表报名称
     */
    excelName: string;
    /**
     * 页数
     */
    pageNum: number[];

    /**
     * 查询
     */
    query: () => void;
    /**
     * 跳转至指定页面
     */
    setPage: (n: number) => void;
    /**
     * 当前显示的页
     */
    currentPage: number;


    /**
     *页面上显示的项
     */
    workExcelList: any;

    /**
     * 显示弹框
     */
    showModal(item: any): void;

    /**
     * 导出
     */
    exportWork(item: any): void;

}

/**查询条件 */
interface IWorkExcelExportQueryTerms {
    /**
     * 表报名称
     */
    excelName: string;
}


class IWorkExcelExportController {
    // 所有的表报导出项
    totalList: WorkExcelItem[];
    // 符合搜索条件的导出项目
    queryedList: WorkExcelItem[];
    // 当前搜索条件下的总页数
    totalPage: number;
    //导出按钮是否双击
    isClicking:boolean=false;

    constructor(private $scope: IWorkExcelExportViewModel, private valueService: IValueService,
        private $ngBootbox: BootboxService, private $state: angular.ui.IStateService, private routeService: routeService, private $location: ng.ILocationService, private exportWorkService: IexportWorkService) {

        this.init();
    }

    /**
     * 初始化
     */
    init(): void {
        this.totalList = this.valueService.getWorkExcelList().data;
        this.$scope.workExcelList = this.getWorkExcelList('', 0, 9);
        this.$scope.showModal = this.showModal;
        this.$scope.queryFunc = this.queryFunc;
        //导出
        this.$scope.exportWork =this.exportWork;

        this.$scope.setPage = this.setPage;
        this.$scope.currentPage = 0;
        
    }

    queryFunc = () => {
        this.$scope.workExcelList = this.getWorkExcelList(this.$scope.excelName, 0, 9);
        this.$scope.currentPage = 0;
    }

    getWorkExcelList = (query: string, startIndex: number, endIndex: number) => {
        this.queryedList = !(query && query.trim()) ? this.totalList : this.totalList.filter(item => { return item.name.indexOf(query.trim()) > -1 });

        this.$scope.pageNum = [];
        this.totalPage = Math.ceil(this.queryedList.length / 9)
        for (var i = 0; i < this.totalPage; i++) {
            this.$scope.pageNum.push(i)
        }
        endIndex = endIndex > this.queryedList.length ? this.queryedList.length : endIndex;
        startIndex = startIndex < 0 ? 0 : startIndex;

        let workExcelList = this.queryedList.slice(startIndex, endIndex);
        workExcelList.forEach(item => {
            item.startTime = this.getTimeOfPreMonth(1);
            item.endTime = this.getTimeOfPreMonth(0);
        });

        return workExcelList;

    }

    /**
     * 获取前n个月的起始时间戳
     * n  当前时间的前n个月
     */
    getTimeOfPreMonth = (n: number): string => {
        const now = new Date();
        const thisYear = now.getFullYear();
        const thisMonth = now.getMonth();
        const theYear = thisMonth + 1 - n > 0 ? thisYear : thisYear - 1;
        let theMonth = thisMonth + 1 - n > 0 ? thisMonth - n + 1 : thisMonth - n + 12 + 1;
        return `${theYear}.${theMonth}.01 00:00`;
    }

    showModal = (item) => {
        this.$scope.excelInfo = item;
    }

    setPage = (n) => {
        if (n > this.totalPage || n < 0 || n == this.totalPage) {
            return;
        }
        this.$scope.workExcelList = this.queryedList.slice(n * 9, (n + 1) * 9);
        //翻页后的默认起始时间和结束时间
        this.$scope.workExcelList.forEach(item => {
            item.startTime = this.getTimeOfPreMonth(1);
            item.endTime = this.getTimeOfPreMonth(0);
        });
        this.$scope.currentPage = n;
    }

    //导出列表
    exportWork=(item)=>{
        if(this.isClicking){
            return;
        }
        this.isClicking=true;
        setTimeout(()=> {
            this.isClicking = false;
        }, 500);

        // console.log('item',item)
        var st=new Date(item.startTime).getTime();
        var et=new Date(item.endTime).getTime();
        
        if(!item.startTime || !item.endTime || st>et){
            if(!item.startTime){
                this.$ngBootbox.alert("请选择发货起始时间");
            }
            if(!item.endTime){
                this.$ngBootbox.alert("请选择发货结束时间");
            }
            else{
                this.$ngBootbox.alert("请选择的“发货起始时间”要在“发货结束时间”之前");
            }
            // this.$ngBootbox.alert("请选择发货起始时间和结束时间");
        }else{
            switch (item.name) {
                case "锐光运费":{ //江苏锐光车业有限公司  f6a689eb-1730-4dc2-bfcb-8d62fe6cb151                    
                    this.exportWorkService.receivableReportRG(item.startTime,item.endTime,'f6a689eb-1730-4dc2-bfcb-8d62fe6cb151');
                } break;
                case "亚洲创建对账单":{ //亚洲创建（河源）木业有限公司 56635b5d-b10e-4a76-baa9-cb394ce0400a                    
                    this.exportWorkService.receivableReportYZCJ(item.startTime,item.endTime,'56635b5d-b10e-4a76-baa9-cb394ce0400a');
                } break;
                case "平安":{  //江苏平安汽车部件有限公司 53fc2b71-138f-439a-a772-ec88ea92ee16                    
                    this.exportWorkService.receivableReportPA(item.startTime,item.endTime,'53fc2b71-138f-439a-a772-ec88ea92ee16');
                } break;
                case "丹阳彤明高科汽车电器有限公司界牌分公司": { //江苏彤明高科汽车电器有限公司 86860493-7410-4f8f-abfc-19966c806bf2                    
                    this.exportWorkService.receivableReportTM(item.startTime,item.endTime,'86860493-7410-4f8f-abfc-19966c806bf2');
                } break;
                case "苏阳运费": { //丹阳市苏阳汽车装饰有限公司  92e77217-de64-4170-8a21-b7e405fd2925                    
                    this.exportWorkService.receivableReportSY(item.startTime,item.endTime,'92e77217-de64-4170-8a21-b7e405fd2925');
                } break;
                case "青山": { //福建省青山纸业股份有限公司 a6daafd4-7bd3-451b-b70c-d4f6b6f2ffb5                    
                    this.exportWorkService.receivableReportQS(item.startTime,item.endTime,'a6daafd4-7bd3-451b-b70c-d4f6b6f2ffb5');
                } break;
                case "卡威运费": { //江苏卡威汽车工业集团有限公司 31dc8172-fc23-46df-a8c4-614a59f8c833
                    this.exportWorkService.receivableReportKW(item.startTime,item.endTime,'31dc8172-fc23-46df-a8c4-614a59f8c833');
                } break;
                case "华宇运费": { //江苏华宇灯具有限公司  18ec82a7-547a-485a-9555-ed0950982b08                    
                    this.exportWorkService.receivableReportHY(item.startTime,item.endTime,'18ec82a7-547a-485a-9555-ed0950982b08');
                } break;
                case "华凯对账单": { //江苏华凯比克希线束有限公司  db0aab42-63b6-4ab5-902d-baaa23a6c35d                    
                    this.exportWorkService.receivableReportHK(item.startTime,item.endTime,'db0aab42-63b6-4ab5-902d-baaa23a6c35d');
                } break;
    
                //第二页
                case "天诚运费": { //江苏天诚车饰科技有限公司  c49404fa-0da3-41fd-a2c0-1219254d4f7e                    
                    this.exportWorkService.receivableReportTC(item.startTime,item.endTime,'c49404fa-0da3-41fd-a2c0-1219254d4f7e');
                } break;
                case "秦龙运费": { //江苏秦龙汽车科技有限公司  238f2759-0fbe-482b-8087-5ddfc52b703a                    
                    this.exportWorkService.receivableReportQL(item.startTime,item.endTime,'238f2759-0fbe-482b-8087-5ddfc52b703a');
                } break;
                case "鼎盛对账单": { //镇江鼎胜铝业股份有限公司  c6d3124f-5d10-40c4-8798-d403d38e1850                    
                    this.exportWorkService.receivableReportDS(item.startTime,item.endTime,'c6d3124f-5d10-40c4-8798-d403d38e1850');
                } break;
                case "联创对账单": { //丹阳市联创汽车配件有限公司 c45fd66d-75a5-445d-9c32-528c915504b5                    
                    this.exportWorkService.receivableReportLC(item.startTime,item.endTime,'c45fd66d-75a5-445d-9c32-528c915504b5');
                } break;
                case "谊善运费": { //丹阳谊善车灯设备制造有限公司  ba4cb2e2-7788-4004-a87c-4ec3bd8710f1                    
                    this.exportWorkService.receivableReportYS(item.startTime,item.endTime,'ba4cb2e2-7788-4004-a87c-4ec3bd8710f1');
                } break;
                case "四建对账单": { //镇江四建建设有限公司 6a35aaeb-8ad3-4efa-8af3-06e4957959b5
                    this.exportWorkService.receivableReportSJ(item.startTime,item.endTime,'6a35aaeb-8ad3-4efa-8af3-06e4957959b5');
                } break;
                case "中船动力有限公司运费结算清单": { //中船动力有限公司 39bcbfb9-fbcc-486f-a92b-51b46f4bb1c1                    
                    this.exportWorkService.receivableReportZCDL(item.startTime,item.endTime,'39bcbfb9-fbcc-486f-a92b-51b46f4bb1c1');
                } break;
                case "盛达对账单": { //镇江市盛达冷轧焊管有限责任公司  6e5f453b-8016-43fd-9105-6073f77765c5                    
                    this.exportWorkService.receivableReportSD(item.startTime,item.endTime,'6e5f453b-8016-43fd-9105-6073f77765c5');
                } break;
                case "晶谷对账单": { //江苏晶谷米机有限公司 7fc6f3e7-e22b-4d26-ad31-ebf6b77e4302                    
                    this.exportWorkService.receivableReportJG(item.startTime,item.endTime,'7fc6f3e7-e22b-4d26-ad31-ebf6b77e4302');
                } break;
    
                //第三页
                case "华峰电器对账单": { //华峰电器有限公司  f8e53bff-3635-40dc-941e-ae2953bf097e
                    this.exportWorkService.receivableReportHF(item.startTime,item.endTime,'f8e53bff-3635-40dc-941e-ae2953bf097e');
                } break;
                case "上海坚诺对账单": { //上海坚诺出口有限公司  66306d70-ec53-4e66-847a-aa20607dfc8a
                    this.exportWorkService.receivableReportJN(item.startTime,item.endTime,'66306d70-ec53-4e66-847a-aa20607dfc8a');
                } break;
                case "常州贤平化工对账单": { //常州市贤平化工有限公司 cbf282e3-b1da-444d-b7ac-24e21a40226e                    
                    this.exportWorkService.receivableReportXP(item.startTime,item.endTime,'cbf282e3-b1da-444d-b7ac-24e21a40226e');
                } break;
                case "祥恒（厦门）包装有限公司对账明细": { //祥恒（厦门）包装有限公司  4c161ea7-b104-4c40-9db6-f0fed42ce902                    
                    this.exportWorkService.receivableReportXH(item.startTime,item.endTime,'4c161ea7-b104-4c40-9db6-f0fed42ce902');
                } break;
                case "中欧威能对账单": { //南京中欧威能新能源动力系统有限公司  f17efdbc-79b3-4a02-9135-e83e55264765                    
                    this.exportWorkService.receivableReportWN(item.startTime,item.endTime,'f17efdbc-79b3-4a02-9135-e83e55264765');
                } break;
                case "大亚滤材对账单": { //江苏大亚滤嘴材料有限公司 140bcfa3-b324-4074-8153-fa6348c8d40f                    
                    this.exportWorkService.receivableReportDY(item.startTime,item.endTime,'140bcfa3-b324-4074-8153-fa6348c8d40f');
                } break;
                case "英利对账单": { //英利新能源有限公司 6f9e3308-85ce-4b20-91e9-007324a5297d                    
                    this.exportWorkService.receivableReportYL(item.startTime,item.endTime,'6f9e3308-85ce-4b20-91e9-007324a5297d');
                } break;
                case "英科运费": { //江苏英科医疗制品有限公司 643b3ffc-dc44-4891-a372-6beac53339bb                    
                    this.exportWorkService.receivableReportYK(item.startTime,item.endTime,'643b3ffc-dc44-4891-a372-6beac53339bb');
                } break;
                case "中节能": { //中节能太阳能科技（镇江）有限公司  83918308-3183-43c7-99c3-2a5fc288cda1                    
                    this.exportWorkService.receivableReportZJN(item.startTime,item.endTime,'83918308-3183-43c7-99c3-2a5fc288cda1');
                } break;
    
                //第四页
                case "徐工": { //徐州徐工智联物流服务有限公司  0d79e48a-5731-4d37-bd6d-54fc6e6a3515                    
                    this.exportWorkService.receivableReportXG(item.startTime,item.endTime,'0d79e48a-5731-4d37-bd6d-54fc6e6a3515');
                } break;
                case "威力": { //江苏勇进电力设备有限公司（原江苏威力钢构有限公司）  65cac6f4-aa0d-411c-8029-f1c6dc7e5232                    
                    this.exportWorkService.receivableReportWL(item.startTime,item.endTime,'65cac6f4-aa0d-411c-8029-f1c6dc7e5232');
                } break;
                
                //11.30新增六张表
                case "天工对账单":{
                    this.exportWorkService.receivableReportTG(item.startTime,item.endTime,'d77f184b-e1cc-4779-a81f-fe3ea80e8dba');
                } break;

                case "振光电力":{
                    this.exportWorkService.receivableReportZGDL(item.startTime,item.endTime,'6f0fb7b0-774b-47b6-9ad1-97143a449d6c');
                } break;

                case "沃得机械对账单":{
                    this.exportWorkService.receivableReportWDJX(item.startTime,item.endTime,'a41b5e09-8048-433a-af01-f48059fadd3a');               
                } break;

                case "扬中大全新能源发运明细":{
                    this.exportWorkService.receivableReportTL(item.startTime,item.endTime,'dab73df0-5c71-4e21-ae98-29e1133c3444');                                     
                } break;

                case "中储粮小包装运费结算明细":{
                    this.exportWorkService.receivableReportZCL(item.startTime,item.endTime,'637e11ca-b5c3-426c-9939-c892d5b8dc1a');
                } break;

                case "恒顺对账单":{
                    this.exportWorkService.receivableReportHS(item.startTime,item.endTime,'7ddc9106-291b-4dea-b4a8-8d29a6b50cd8');
                } break;

                case "沃得精机运输对账明细":{//新增 18-1-17 沃得精机（中国）有限公司
                    this.exportWorkService.receivableReportWD(item.startTime,item.endTime,'5c043522-eaac-440a-895a-e29263d72066');
                } break;

                //第五页
                case "中粮东海结算明细表":{//新增 18-3-1
                    this.exportWorkService.receivableReportZLDH(item.startTime,item.endTime,'995fe9fb-9c26-46d7-865a-074e662f51c3');
                } break;

                case "提现表":{//新增 18-1-17
                    this.exportWorkService.receivableReportWithdrawalsFee(item.startTime,item.endTime);
                } break;

                case "回访表（运维）": {
                    this.exportWorkService.visitReport(item.startTime,item.endTime);
                } break;
    
            }
        }
        
    }

}

angular.module("tms").controller("WorkExcelExportController", ["$scope", "valueService", "$ngBootbox", "$state", "routeService", "$location", "exportWorkService", IWorkExcelExportController]);

